package com.ljy.jymail.authserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ljy.common.constant.AuthServerConstant;
import com.ljy.common.utils.HttpUtils;
import com.ljy.common.utils.R;
import com.ljy.jymail.authserver.Feign.MemberFeignService;
import com.ljy.common.vo.MemberRespVo;
import com.ljy.jymail.authserver.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
@Slf4j
public class Oauth2Controller {

    @Autowired
    MemberFeignService memberFeignService;


    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code换取AccessToken
        HashMap<String, String> map = new HashMap<>();
        map.put("client_id","3425351720");
        map.put("client_secret","7c6971bcf4e550fb15738cc5096a33ab");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","https://48u4939222.eicp.vip/auth/oauth2.0/weibo/success");
        map.put("code",code);
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post",
                new HashMap<String, String>(), map,new HashMap<String, String>());

        //换取信息--详细处理
        //获取响应状态行，如果成功，值为200
        if(response.getStatusLine().getStatusCode()==200){
            //获取到了ACCESS_TOKEN
            //获取响应实体 json格式
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);

            //知道当前是哪个社交用户
            //如果当前用户是第一次进入，就自动为当前会员注册，否则就登陆
            //调用远程服务，接收社交用户，判断登录或者注册,使用返回的uid来判断
            R oauthLogin = memberFeignService.oauthLogin(socialUser);
            if (oauthLogin.getCode()==0){
                MemberRespVo data = oauthLogin.getData("data", new TypeReference<MemberRespVo>() {});
                //1、第一次使用session，命令浏览器保存卡号、JSESSIONID这个cookie
                //以后浏览器访问哪个网站都会带上这个网站的cookie
                //子域之间：jymail.com,auth.jymail.com,order.jymail.com
                //发卡的时候(指定域名为父域名)，即使是子域系统发的卡，也能给其他域使用

                //TODO:1、默认发的令牌。session=dsajkdjl；作用域：当前域：(解决子域共享问题)
                //TODO:2、以json的序列号方式存入redis，这样更加方便编写代码和查看
                session.setAttribute(AuthServerConstant.LOGIN_SUCCESS_USER,data);
//                //设置作用域
//                new Cookie("JSESSIONID","").setDomain();
//                response.addCookie()
                return "redirect:https://48u4939222.eicp.vip";
            }else {
                return "redirect:https://48u4939222.eicp.vip/auth/login.html";
            }
        }else {
            //登陆失败
            return "redirect:https://48u4939222.eicp.vip/auth/login.html";
        }


    }
}
